/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.Scene;
import czStvDpds.graphics3D.geometry.Mesh;
import czStvDpds.graphics3D.geometry.Object3D;
import czStvDpds.graphics3D.geometry.Texture;
import java.nio.FloatBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.UnicodeFont;

/**
 *
 * @author divad
 */
public class GUIObject extends Object3D {

    @Deprecated
    private String text;
    @Deprecated
    private float textX, textY;
    private TextPiece[] cuttedText;
    private UnicodeFont font;
    private Scene drawer;
    private Texture usedTexture;

    public GUIObject(Mesh data) {
        super(data);
        super.enabled = false;
    }

    @Override
    protected void predrawSetup() {
        Matrix4f posMat = super.getPosMatrix();
        float size = drawer.getRelativeWidth();
        float x = posMat.m30;
        float y = posMat.m31;
        if (posMat.m00 != size) {
            float oldScale = posMat.m00;
            posMat.setIdentity();
            posMat.m30 = (x / oldScale) * size;
            posMat.m31 = (y / oldScale) * size;
            posMat.m32 = -2;
            posMat.scale(new Vector3f(size, size, size));
        } else if (x < 1 && x > 0) {
            posMat.m30 = x * size;
            posMat.m31 = y * size;
            posMat.m32 = -2;
        }
    }

    @Override
    protected void secondaryDraw() {
        if (font != null) {
            GraphicsIndex.getGLProperties().getTextureUnitSettings(0).adjustGLToText();
            GL11.glLoadIdentity();
            GL11.glTranslatef(0, 0, -2);
            if (this.cuttedText == null && this.text != null && this.text.length() <= TextPiece.MAX_LENGTH) {
                font.drawString(super.getPosMatrix().m30 + this.textX, super.getPosMatrix().m31 + textY, this.text);
            } else if (cuttedText != null) {
                for (TextPiece textPiece : this.cuttedText) {
                    font.drawString(super.getPosMatrix().m30 + textPiece.getX(), super.getPosMatrix().m31 + textPiece.getY(), textPiece.getText());
                }
            }
        }
    }

    public void setPosition(float x, float y) {
        super.getPosMatrix().m30 = x;
        super.getPosMatrix().m31 = y;
        super.getPosMatrix().m32 = -2;
    }

    public void resize(FloatBuffer geometryData) {
        GraphicsIndex.getGLSynchronizer().changeVBOData(this, geometryData, 0);
    }

    public UnicodeFont getFont() {
        return font;
    }

    public String getText() {
        return text;
    }

    public float getTextX() {
        return textX;
    }

    public float getTextY() {
        return textY;
    }

    public void setFont(UnicodeFont font) {
        this.font = font;
    }

    public void setText(TextPiece[] text) {
        this.cuttedText = text;
    }

    @Deprecated
    public void setText(String text) {
        this.text = text;
    }

    @Deprecated
    public void setTextX(float textX) {
        this.textX = textX;
    }

    @Deprecated
    public void setTextY(float textY) {
        this.textY = textY;
    }

    public Scene getDrawer() {
        return drawer;
    }

    public void setDrawer(Scene drawer) {
        if (this.drawer != null && drawer != this.drawer) {
            this.drawer.removeObject(this);
            if (drawer != null) {
                drawer.addObject(this, false);
            }
        } else if (this.drawer == null && drawer != null) {
            drawer.addObject(this, false);
        }
        this.drawer = drawer;
    }

    public void setTexture(Texture texture) {
        if (this.usedTexture != texture) {
            super.removeTexture(usedTexture);
            super.addTexture(texture, 0);
            usedTexture = texture;
        }
    }
}
